//
//  DYLoginView.m
//  kafuMerchant
//
//  Created by 帝云科技 on 2018/9/10.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYLoginView.h"
#import "DYLoginTextFieldView.h"

@interface DYLoginView ()

@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *contentLabel;
@property (nonatomic,strong) UIButton *registerButton;
@property (nonatomic,strong) DYLoginTextFieldView *phoneText;
@property (nonatomic,strong) DYLoginTextFieldView *passwordText;
@property (nonatomic,strong) UIButton *forgotPasswordButton;
@property (nonatomic,strong) UIButton *loginButton;


@end



@implementation DYLoginView

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardDidShowNotification object:nil];
        [center addObserver:self selector:@selector(keyboardDidHide) name:UIKeyboardWillHideNotification object:nil];
        
//        [self.phoneText.textField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
//        [self.passwordText.textField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
        
        [self initUI];
    }
    return self;
}
-(void)initUI
{
    [self addSubview:self.scrollView];
    [self.scrollView addSubview:self.titleLabel];
    [self.scrollView addSubview:self.contentLabel];
    [self.scrollView addSubview:self.registerButton];
    [self.scrollView addSubview:self.phoneText];
    [self.scrollView addSubview:self.passwordText];
    [self.scrollView addSubview:self.forgotPasswordButton];
    [self.scrollView addSubview:self.loginButton];
    
    
    [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(0);
    }];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(BILIWIDTH(18));
        make.top.equalTo(BILIHEIGHT(40));
    }];
    
    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.top.equalTo(self.titleLabel.bottom).offset(BILIHEIGHT(10));
    }];
    
    [self.registerButton makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentLabel.right).offset(BILIWIDTH(5));
        make.centerY.equalTo(self.contentLabel);
    }];
    
    [self.phoneText makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(BILIWIDTH(-18));
        make.height.equalTo(BILIHEIGHT(50));
        make.top.equalTo(self.contentLabel.bottom).offset(BILIHEIGHT(60));
    }];
    
    [self.passwordText makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.phoneText);
        make.height.equalTo(self.phoneText);
        make.top.equalTo(self.phoneText.bottom).offset(BILIHEIGHT(8));
    }];
    
    [self.forgotPasswordButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordText.bottom).offset(BILIHEIGHT(40));
        make.left.equalTo(self.titleLabel);
    }];
    
    [self.loginButton makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.phoneText);
        make.centerY.equalTo(self.forgotPasswordButton);
        make.size.equalTo(CGSizeMake(BILIWIDTH(98), BILIHEIGHT(38)));
    }];
    
    [self layoutIfNeeded];
}
-(void)buttonClick:(UIButton *)button
{
    if (button==self.loginButton) {
        if (![NSString xhq_phoneFormatCheck:self.phoneText.textField.text]) {
            XHQHUDTEXT(@"请输入正确的手机号");
        }
        XHQHUDTEXT(@"登录");
    }else if (button==self.registerButton){
        !self.registerBlock ? : self.registerBlock();
    }else if (button==self.forgotPasswordButton){
        ! self.forgotPasswordBlock ? : self.forgotPasswordBlock();
    }
}

-(void)textFieldChange
{
    if (self.phoneText.textField.text.length>0&&self.passwordText.textField.text.length>0) {
        self.loginButton.enabled=YES;
        self.loginButton.backgroundColor=[UIColor xhq_base];
    }else{
        self.loginButton.enabled=NO;
        self.loginButton.backgroundColor=[UIColor xhq_default];
    }
}
-(void)keyboardDidShow{
}
-(void)keyboardDidHide
{
    self.scrollView.contentOffset=CGPointMake(0,0);
}
-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView=[[UIScrollView alloc]init];
        _scrollView.alwaysBounceVertical=YES;
        _scrollView.scrollEnabled=NO;
    }
    return _scrollView;
}
-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel=[Utils labelWithTitle:@"您好，" titleFontSize:30 textColor:ColorTitle backgroundColor:WhiteColor alignment:0];
    }
    return _titleLabel;
}

-(UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel=[Utils labelWithTitle:@"欢迎来到卡福商家，立即" titleFontSize:16 textColor:[UIColor xhq_assist] backgroundColor:WhiteColor alignment:0];
    }
    return _contentLabel;
}

-(UIButton *)registerButton
{
    if (!_registerButton) {
        _registerButton=[Utils createBtnWithType:UIButtonTypeCustom backgroundColor:WhiteColor action:@selector(buttonClick:) target:self title:@"注册" image:nil font:16 textColor:[UIColor xhq_base]];
    }
    return _registerButton;
}


-(DYLoginTextFieldView *)phoneText
{
    if (!_phoneText) {
        _phoneText=[[DYLoginTextFieldView alloc]init];
        _phoneText.textField.placeholder=@"请输入手机号";
        _phoneText.titleLabel.text=@"手机号";
        [_phoneText.textField setNumbersKeyboard];
        @weakify(self);
        _phoneText.topBlock = ^{
            @strongify(self);
            self.scrollView.contentOffset=CGPointMake(0,self.phoneText.xhq_y-BILIHEIGHT(60));
        };
        _phoneText.textFieldChangeBlock = ^(NSString *text) {
            @strongify(self);
            [self textFieldChange];
        };
    }
    return _phoneText;
}
-(DYLoginTextFieldView *)passwordText
{
    if (!_passwordText) {
        _passwordText=[[DYLoginTextFieldView alloc]init];
        _passwordText.textField.placeholder=@"请输入密码";
        _passwordText.titleLabel.text=@"密码";
        _passwordText.isHide=NO;
        @weakify(self);
        _passwordText.topBlock = ^{
            @strongify(self);
            self.scrollView.contentOffset=CGPointMake(0,self.phoneText.xhq_y-BILIHEIGHT(60));
        };
        _passwordText.textFieldChangeBlock = ^(NSString *text) {
            @strongify(self);
            [self textFieldChange];
        };
    }
    return _passwordText;
}
-(UIButton *)forgotPasswordButton
{
    if (!_forgotPasswordButton) {
        _forgotPasswordButton=[Utils createBtnWithType:UIButtonTypeCustom backgroundColor:WhiteColor action:@selector(buttonClick:) target:self title:@"忘记密码" image:nil font:13 textColor:[UIColor xhq_assist]];
    }
    return _forgotPasswordButton;
}
-(UIButton *)loginButton
{
    if (!_loginButton) {
        _loginButton=[Utils createBtnWithType:UIButtonTypeCustom backgroundColor:[UIColor xhq_default] action:@selector(buttonClick:) target:self title:@"登录" image:@"icon_login" font:17 textColor:[UIColor whiteColor]];
        [_loginButton placeImageTitlePosition:ZYButtonImagePositionRight space:BILIWIDTH(15)];
        _loginButton.enabled=NO;
        [_loginButton xhq_cornerRadius:BILIHEIGHT(19)];
    }
    return _loginButton;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
